﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectInRange : MonoBehaviour
{
    public ShapeType shapeType;
    public int range;
    private PolygonCollider2D _collider2D;

    private Dictionary<int, GameObject> inRangeObject = new Dictionary<int, GameObject>();

    public delegate void ObjectIncomeNotify(GameObject other);

    public ObjectIncomeNotify objectIncomeEvent;
    
    public void InitialCollider()
    {
        _collider2D = GetComponent<PolygonCollider2D>();
    }

    public Dictionary<int, GameObject> GetIncludeObject()
    {
        return inRangeObject;
    }

    public void FlushColliderZone(int newRange)
    {
        range = newRange;
        FlushColliderZone();
    }
    
    public void FlushColliderZone()
    {
        switch (shapeType)
        {
            case ShapeType.Star:
                _collider2D.points = new []
                {
                    new Vector2(0f, 1f * range),
                    new Vector2(-1f * range, 0f),
                    new Vector2(0f, -1f * range),
                    new Vector2(1f * range, 0f)
                };

                break;
            case ShapeType.Triangle:
                _collider2D.points = new []
                {
                    new Vector2(1f, 0f),
                    new Vector2(1f * range, 1f * range * .45f),
                    new Vector2(1f * range, -1f * range * .45f)
                };

                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
    
    private void OnTriggerEnter2D(Collider2D other)
    {
        objectIncomeEvent?.Invoke(other.gameObject);
    }
    
}

public enum ShapeType
{
    Star,
    Triangle
}